﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Coolite.Ext.Web;
using Newtonsoft.Json;

namespace System.Web.Mvc
{
    public class AjaxStoreResult : ActionResult
    {
        public AjaxStoreResult() { }

        public AjaxStoreResult(object data)
        {
            this.Data = data;
        }

        public AjaxStoreResult(object data, int totalCount)
            : this(data)
        {
            this.TotalCount = totalCount;
        }

        public AjaxStoreResult(object data, int totalCount, string debug)
            : this(data, totalCount)
        {
            this.Debug = debug;
        }

        public AjaxStoreResult(StoreResponseFormat responseFormat)
        {
            this.ResponseFormat = responseFormat;
        }

        private object data;
        public object Data
        {
            get { return this.data; }
            set { this.data = value; }
        }

        private int totalCount = -1;
        public int TotalCount
        {
            get { return this.totalCount; }
            set { this.totalCount = value; }
        }

        private string debug;
        public string Debug
        {
            get { return this.debug; }
            set { this.debug = value; }
        }

        private StoreResponseFormat responseFormat = StoreResponseFormat.Load;
        public StoreResponseFormat ResponseFormat
        {
            get { return this.responseFormat; }
            set { this.responseFormat = value; }
        }

        private SaveStoreResponse saveResponse;
        public SaveStoreResponse SaveResponse
        {
            get
            {
                if (this.saveResponse == null)
                {
                    this.saveResponse = new SaveStoreResponse();
                }
                return this.saveResponse;
            }
        }

        public static List<JsonConverter> jc = new List<JsonConverter>(new JsonConverter[] { new Newtonsoft.Json.Converters.JavaScriptDateTimeConverter() }); 

        public override void ExecuteResult(ControllerContext context)
        {
            switch (this.ResponseFormat)
            {
                case StoreResponseFormat.Load:

                    string json = string.Empty;

                    string debugMsg = this.debug.IsNullOrEmpty() ? string.Empty : string.Format(",debug:'{0}'", this.debug);

                    try
                    {
                        json = Coolite.Ext.Web.JSON.Serialize(Data, jc);

                        if (this.totalCount != -1)
                        {
                            json = "{data:" + json + ", totalCount:" + this.totalCount + ",success:true,errors:{}" + debugMsg + "}";
                        }
                    }
                    catch (Exception e)
                    {
                        json = "{success:false,errors:{info:'" + e.InnerException.Message + "'}" + debugMsg + "}";
                    }

                    context.HttpContext.Response.Write(json);

                    break;
                case StoreResponseFormat.Save:

                    Response response = new Response(true);
                    response.Success = this.SaveResponse.Success;
                    response.Msg = this.SaveResponse.ErrorMessage;
                    StoreResponseData saveResponse = new StoreResponseData();
                    saveResponse.Confirmation = this.SaveResponse.ConfirmationList;
                    response.Data = saveResponse.ToString();

                    response.Write();
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }
        }
    }

    public enum StoreResponseFormat
    {
        Load,
        Save
    }

    public class SaveStoreResponse
    {
        private bool success = true;
        private string errorMessage;

        public bool Success
        {
            get { return this.success; }
            set { this.success = value; }
        }

        public string ErrorMessage
        {
            get { return this.errorMessage; }
            set { this.errorMessage = value; }
        }

        public ConfirmationList ConfirmationList { get; set; }
    }
}
